package com.teamelite.exmgmt;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class cMainMenu extends TabActivity {
	private String id_pengguna;
	private ArrayList<arrList> lstPengguna;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// Ambil ID Pengguna
		this.id_pengguna = getIntent().getExtras().getString("id_pengguna");
		
		DBHelper db = new DBHelper(this);
		lstPengguna = db.getPengguna(this.id_pengguna);
		setTitle("User : "+lstPengguna.get(0).nama);
		
		// Buat Tab Baru
		final TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("tab1")
        .setIndicator("Activity Traces",getResources().getDrawable(R.drawable.lists))
        .setContent(new Intent(this, cGroupListAktivitas.class).putExtra("id_pengguna", this.id_pengguna)));
        
        tabHost.addTab(tabHost.newTabSpec("tab2")
        .setIndicator("Activity",getResources().getDrawable(R.drawable.activity))
        .setContent(new Intent(this, cMenuAktivitas.class).putExtra("id_pengguna", this.id_pengguna)));
        
        tabHost.addTab(tabHost.newTabSpec("tab3")
        .setIndicator("Summary",getResources().getDrawable(R.drawable.summary))
        .setContent(new Intent(this, cSummary.class).putExtra("id_pengguna", this.id_pengguna)));
        
        tabHost.setBackgroundColor(Color.BLACK);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.tab_optionmenu, menu);
        return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()){
		case R.id.mChangeProfile:
			AlertDialog myAlertDialog;
			LayoutInflater factory = LayoutInflater.from(this);
            final View editView = factory.inflate(R.layout.layout_edituser, null);
            final EditText txtNama = (EditText)editView.findViewById(R.id.txtNama);
            final EditText txtUmur = (EditText)editView.findViewById(R.id.txtUmur);
            final Spinner cbGender = (Spinner)editView.findViewById(R.id.cbGender);
            final EditText txtTinggi = (EditText)editView.findViewById(R.id.txtTinggi);
            final EditText txtBerat = (EditText)editView.findViewById(R.id.txtBerat);
            // Get Data From DB For Editing
            txtNama.setText(lstPengguna.get(0).nama);
         	if(lstPengguna.get(0).gender.equals("L")){
         		cbGender.setSelection(0);
         	}else{
         		cbGender.setSelection(1);
         	}
            txtUmur.setText(lstPengguna.get(0).umur);
            txtTinggi.setText(lstPengguna.get(0).tinggi);
            txtBerat.setText(lstPengguna.get(0).berat);
            
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Profile Editor");
            builder.setView(editView);
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// Do Nothing
				}
			});
            builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            	@Override
                public void onClick(DialogInterface dialog, int arg1) {
            		if (txtNama.getText().toString().equals("") || 
            				txtUmur.getText().toString().equals("") || 
            				txtTinggi.getText().toString().equals("") || 
            				txtBerat.getText().toString().equals("")){
            			Toast.makeText(cMainMenu.this, "Please fill all form correctly.", Toast.LENGTH_SHORT).show();
            		}else{
	            		DBHelper db = new DBHelper(cMainMenu.this);
	            		ContentValues values = new ContentValues();
	            		Object selectedGender = cbGender.getSelectedItem();
	            		String gender;
	        			// Isi Gender
	        			if(selectedGender.equals("Laki-Laki")){gender = "L";}else{gender = "P";}
	            		values.put("nama", txtNama.getText().toString());
	            		values.put("jk", gender.toString());
	            		values.put("umur", Integer.parseInt(txtUmur.getText().toString()));
	            		values.put("tinggi", Integer.parseInt(txtTinggi.getText().toString()));
	            		values.put("berat", Integer.parseInt(txtBerat.getText().toString()));
	            		db.updatePengguna(values, cMainMenu.this.id_pengguna);
	            		Toast.makeText(cMainMenu.this, "Data has updated.", Toast.LENGTH_SHORT).show();
	            	}
            	}
            });
            builder.setCancelable(false);
            myAlertDialog = builder.create();
            myAlertDialog.show();
			return true;
		case R.id.mAbout:
			intent = new Intent(this,About.class);
        	startActivity(intent);
			return true;
        case R.id.mExit:
        	intent = new Intent(Intent.ACTION_MAIN);
        	intent.addCategory(Intent.CATEGORY_HOME);
        	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        	startActivity(intent);
        	finish();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
	}
}
